package com.liang.design;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * @author lsm
 * @ClassName MQAdapter.java
 * @Description mq消息适配
 * @createTime 2023年12月03日 20:15:00
 */
public class MQAdapter {

    //link中key是RebateInfo的属性，value是原对象属性
    public static RebateInfo filter(String jsonStr, Map<String,String> link) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return filter(JSON.parseObject(jsonStr,Map.class),link);
    }

    public static RebateInfo filter(Map obj, Map<String,String> link) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        RebateInfo rebateInfo = new RebateInfo();
        Set<String> keySet = link.keySet();
        for (String key : keySet) {
            Object val = obj.get(link.get(key));
            RebateInfo.class
                    .getMethod("set" + key.substring(0, 1).toUpperCase() + key.substring(1),String.class)
                    .invoke(rebateInfo,val.toString());

        }
        return rebateInfo;
    }
}
